package com.haredot.service.impl;

import com.haredot.dto.PasswordDTO;
import com.haredot.entity.Student;
import com.haredot.entity.Teacher;
import com.haredot.entity.User;
import com.haredot.mapper.StudentMapper;
import com.haredot.mapper.TeacherMapper;
import com.haredot.service.IndexService;
import com.haredot.utils.MD5Utils;
import com.haredot.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class IndexServiceImpl implements IndexService {

    @Resource
    private TeacherMapper teacherMapper ;

    @Resource
    private StudentMapper studentMapper ;

    @Override
    public Map<String, Integer> queryStudentAndTeacherCount() {
        // 查询学生总人数 和 教师总人数
        return teacherMapper.queryStudentAndTeacherCount();
    }

    @Override
    public Map<String, Object> queryData() {
        Map<String, Object> ret = new HashMap<>();
        // 查询 每一门课程选修的人数
        List<Map<String, Integer>> list = teacherMapper.queryCourseAndStuCount();
        // 将数据库查询到的数据 进行转换，返回一个 Map<String, List<Integer>> 格式的数据
        Map<String, List<?>> courseCount = list.stream().reduce(new HashMap<>(), (map, data) -> {
            if (map.isEmpty()) {
                List<Integer>  countlist = new ArrayList<>();
                countlist.add(Integer.valueOf(String.valueOf(data.get("count")))) ;
                List<String>  courseList = new ArrayList<>();
                courseList.add(String.valueOf(data.get("course")));
                map.put("courseList", courseList);
                map.put("countList", countlist);
            }else{
                List<String> courseList = (List<String>)map.get("courseList");
                List<Integer>  countlist = (List<Integer>) map.get("countList");
                courseList.add(String.valueOf(data.get("course")));
                countlist.add(Integer.valueOf(String.valueOf(data.get("count"))));
            }
            return map ;
        }, (a, b)-> a);

        ret.putAll(courseCount);

        // 查询 每个专业 学生人数
        List<Map<String, Integer>> majorList = teacherMapper.queryMajorAndStuCount();

        ret.put("majorList", majorList);

        return ret ;
    }


    @Override
    public R<?> modifyPassword(User current, PasswordDTO passwordDTO) {
        // 根据当前用户， 获取 原数据库密码
        if (!MD5Utils.md5(passwordDTO.getPassword()).equals(current.getPassword())) {
            // 提示原密码错误
            return R.fail().data(Map.of("password", "原密码不正确"));
        }
        // 修改密码为新密码
        if ("teacher".equals(current.getRole())) {
            Teacher teacher = Teacher.builder()
                    .id(current.getUsername())
                    .password(passwordDTO.getNewPassword())
                    .build();
            // 修改密码
            teacherMapper.updatePassword(teacher);
        }else{
            Student student = Student.builder()
                    .id(current.getUsername())
                    .password(passwordDTO.getNewPassword())
                    .build();

            studentMapper.updatePassword(student);
        }

        return R.ok().build();
    }
}
